﻿using MES_SystemUI.Domain;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace MES_SystemUI.UI
{
    public partial class ProductionProcessesPutin : MES_SystemUI.UI.Putin
    {
        public ProductionProcessesPutin()
        {
            InitializeComponent();
            this.Load += ProductionProcessesPutin_Load;
            textBox2.Click += TextBox2_Click;
        }

        private void TextBox2_Click(object sender, EventArgs e)
        {
            textBox2.Text = "GX" + DateTime.Now.ToString("yyyyMMddHHmmss");
        }

        private void ProductionProcessesPutin_Load(object sender, EventArgs e)
        {
            RefreshText();
        }

        private void RefreshText()
        {
            textBox1.Text = string.Empty;
            textBox2.Text = string.Empty;
            textBox3.Text = string.Empty;
            richTextBox1.Text = string.Empty;
        }
        public override void PutinData()
        {
            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                MessageBox.Show("工序名称不能为空");
                return;
            }
            if (string.IsNullOrEmpty(textBox2.Text.Trim()))
            {
                MessageBox.Show("工序编码不能为空");
                return;
            }
            if (string.IsNullOrEmpty(textBox3.Text.Trim()))
            {
                MessageBox.Show("工序要求不能为空");
                return;
            }
            using (var c = new Context.MES_SystemContext())
            {
                ProductionProcesse mode = new ProductionProcesse()
                {
                    ProductionProcessName = textBox1.Text,
                    ProductionProcessCode = textBox2.Text,
                    ProcessRequirements = textBox3.Text,
                    ProcessDrawings = pictureBox1.Image==null? null : Helpers.ImageHelper.ImageToByteArray(pictureBox1.Image),
                    Remark = richTextBox1.Text,
                    CreateUserId = SystemDataConfig.LoginUser.UserId,
                };
                c.Entry(mode).State = System.Data.Entity.EntityState.Added;
                c.SaveChanges();
            }
            RefreshText();
            MessageBox.Show("提交成功！");
        }
    }
}
